//
//  ScheduledViewController.m
//  KiklinkApp
//
//  Created by LJC on 14/10/23.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "ScheduledViewController.h"

#import "ImagesManager.h"
#import "UIColor+hex.h"

#import "FillinInfoViewController.h"

@interface ScheduledViewController ()
{
    BOOL startSelected;
    NSDateFormatter *formatter;
}
@end

@implementation ScheduledViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"预约日期";
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];

    
    //NSDateFormatter
    formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    [formatter setDateFormat:@"MM月dd日     EEEE    HH:mm"];
    
    
    //BundleImages
    ImagesManager *imagesManager = [ImagesManager sharedImagesManager];
    
    
    //leftNavItem
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, NavBarHeight/4, NavBarHeight/2)];
    [backButton setBackgroundImage:[imagesManager back] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[imagesManager backTapped] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //start&stop
    startSelected = YES;
    
    [self.startView.layer setCornerRadius:3];
    self.startView.layer.borderWidth = 1;
    self.startView.layer.borderColor = [UIColor colorWithHexString:AppColor_Main andAlpha:0.8].CGColor;
    
    [self.stopView.layer setCornerRadius:3];
    self.stopView.layer.borderWidth = 1;
    self.stopView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    UITapGestureRecognizer *startTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(startTapped)];
    startTapped.numberOfTapsRequired = 1;
    [self.startView addGestureRecognizer:startTapped];
    
    UITapGestureRecognizer *stopTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(stopTapped)];
    stopTapped.numberOfTapsRequired = 1;
    [self.stopView addGestureRecognizer:stopTapped];
    
    
    //datePicker
    [self.datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
    
    
    //next
    self.startStr = @"";
    self.stopStr = @"";
    [self.next.layer setCornerRadius:4];
    [self.next addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - back
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - back End

#pragma mark - start&stop tapped
- (void)startTapped
{
    startSelected = YES;
    self.startView.layer.borderColor = [UIColor colorWithHexString:AppColor_Main andAlpha:0.8].CGColor;
    self.stopView.layer.borderColor = [UIColor whiteColor].CGColor;
}
- (void)stopTapped
{
    startSelected = NO;
    self.stopView.layer.borderColor = [UIColor colorWithHexString:AppColor_Main andAlpha:0.8].CGColor;
    self.startView.layer.borderColor = [UIColor whiteColor].CGColor;
}
#pragma mark - start&stop tapped End

#pragma mark - dateChanged
- (void)dateChanged
{
    if (startSelected) {
        self.startTime.text = [formatter stringFromDate:self.datePicker.date];
        self.startStr = self.startTime.text;
    } else {
        self.stopTime.text = [formatter stringFromDate:self.datePicker.date];
        self.stopStr = self.stopTime.text;
    }
}
#pragma mark - dateChanged End

#pragma mark - nextStep
- (void)nextStep
{
    if ([self.startStr isEqualToString:@""] || [self.stopStr isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请完善课程预约时间" message:@"" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        [alert show];
    } else {
        FillinInfoViewController *fillinInfoVC = [[FillinInfoViewController alloc] init];
        fillinInfoVC.productID = self.productID;
        fillinInfoVC.productName = self.productName;
        fillinInfoVC.price = self.price;
        fillinInfoVC.startStr = self.startStr;
        fillinInfoVC.stopStr = self.stopStr;
        [self.navigationController pushViewController:fillinInfoVC animated:YES];
    }
}
#pragma mark - nextStep End

@end
